package com.all.play.myapplication.controller.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.all.play.myapplication.R;
import com.all.play.myapplication.moudle.Model;
import com.all.play.myapplication.moudle.bean.UserInfo;
import com.hyphenate.EMCallBack;
import com.hyphenate.chat.EMClient;
import com.hyphenate.exceptions.HyphenateException;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by 聪明一只哈 on 2018/3/30.
 */

public class LoginActivity extends AppCompatActivity {

    @BindView(R.id.et_login_name)
    EditText etLoginName;
    @BindView(R.id.et_login_pwd)
    EditText etLoginPwd;
    @BindView(R.id.bt_login_regist)
    Button btLoginRegist;
    @BindView(R.id.bt_login_login)
    Button btLoginLogin;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);

    }

    @OnClick({R.id.bt_login_regist, R.id.bt_login_login})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.bt_login_regist:
                registerUser();

                break;
            case R.id.bt_login_login:

                loginUser();

                break;
        }
    }

    /**
     *登录的逻辑
     *@author 聪明一只哈
     *@time 2018/3/30 15:00
     */
    private void loginUser() {

        //1.输入
        final String registerName = etLoginName.getText().toString().trim();
        final String registerPass = etLoginPwd.getText().toString().trim();
        //2.校验
        if(TextUtils.isEmpty(registerName)||TextUtils.isEmpty(registerPass)){
            Toast.makeText(LoginActivity.this, "数值为空", Toast.LENGTH_SHORT).show();
            return;
        }
        //3.登录
        Model.getInstance().getGloableThreadPool().execute(new Runnable() {
            @Override
            public void run() {
                EMClient.getInstance().login(registerName, registerPass, new EMCallBack() {
                    @Override
                    public void onSuccess() {
                        //对模型层数据的处理
                        Model.getInstance().loginSuccess();

                        //保存登录用户的账号
                        Model.getInstance().getUserAccountDao().addAccount(new UserInfo(registerName));



                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {

                                //提示
                                Toast.makeText(LoginActivity.this, "跳转成功", Toast.LENGTH_SHORT).show();

                                //跳转页面
                                startActivity(new Intent(LoginActivity.this,MainActivity.class));

                                finish();
                            }
                        });


                    }

                    @Override
                    public void onError(int i, String s) {
                        Toast.makeText(LoginActivity.this, "登录失败", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onProgress(int i, String s) {

                    }
                });
            }
        });


    }

    /**
     *注册用户信息的逻辑
     *@author 聪明一只哈
     *@time 2018/3/30 15:00
     */
    private void registerUser() {

        //1.输入
        final String registerName = etLoginName.getText().toString().trim();
        final String registerPass = etLoginPwd.getText().toString().trim();
        //2.校验
        if(TextUtils.isEmpty(registerName)||TextUtils.isEmpty(registerPass)){
            Toast.makeText(LoginActivity.this, "数值为空", Toast.LENGTH_SHORT).show();
            return;
        }
        //3.去服务器注册

        //去环信服务器注册账号
        Model.getInstance().getGloableThreadPool().execute(new Runnable() {
            @Override
            public void run() {
                try {
                    EMClient.getInstance().createAccount(registerName,registerPass);
                    //没有异常,则注册成功,更新界面
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(LoginActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
                        }
                    });
                } catch (HyphenateException e) {

                    e.printStackTrace();
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(LoginActivity.this, "注册失败", Toast.LENGTH_SHORT).show();
                        }
                    });
                }
            }
        });

    }
}
